/**
 * 
 */
package hu.gbalage.owlforms.internal.rules;

import hu.gbalage.owlforms.OWLFormsManager;

import java.net.URI;
import java.util.Set;

import org.semanticweb.owl.inference.OWLReasonerException;
import org.semanticweb.owl.model.OWLConstant;
import org.semanticweb.owl.model.OWLDataProperty;
import org.semanticweb.owl.model.OWLIndividual;

/**
 * @author balage
 *
 */
public class EgyForeEsoJovedelemRule implements IRule {

	public static final URI datapropt = URI.create("http://owlforms.gbalage.hu/example/egyforeesojovedelem.owl#hasEgyForeEsoJovedelem");
	
	public static final URI clazz = URI.create("http://owlforms.gbalage.hu/example/egyforeesojovedelem.owl#EgyForeEsoJovedelem");
	
	public static final URI szocpont = URI.create("http://owlforms.gbalage.hu/core.owl#szocpont");
	
	final OWLFormsManager manager;
	
	final OWLDataProperty property;
	
	final OWLDataProperty szocprop;
	
	public EgyForeEsoJovedelemRule(OWLFormsManager manager) {
		this.manager = manager;
		property = manager.getDataFactory().getOWLDataProperty(datapropt);
		szocprop = manager.getDataFactory().getOWLDataProperty(szocpont);
	}
	
	protected int getValue(int data){
		if (data <= 21000) return 63;
		
		return 63-((data-21000)/1500)*2;
	}
	
	/* (non-Javadoc)
	 * @see hu.gbalage.owlforms.internal.rules.IRule#execute(org.semanticweb.owl.model.OWLIndividual)
	 */
	public void execute(OWLIndividual individual) throws OWLReasonerException {
		Set<OWLConstant> data = manager.getReasoner().getDataPropertyRelationships(individual).get(datapropt);
		if (data == null) return;
		if (data.size() == 0) return;

		OWLConstant c = data.iterator().next();
		try{
			int d = Integer.parseInt(c.getLiteral());
			int szoc = getValue(d);
			manager.addTemporary(manager.getDataFactory().getOWLDataPropertyAssertionAxiom(individual, szocprop, szoc));
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see hu.gbalage.owlforms.internal.rules.IRule#runsOn()
	 */
	public URI runsOn() {
		return clazz;
	}

}
